package com.cpkso.buss.backend.config;

import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cpkso.buss.backend.security.AuthRealm;

/**
 * @author wrzhxy@qq.com
 * @date 2018年1月7日
 */
@Configuration
public class ShiroConfig {

	@Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
        daap.setProxyTargetClass(true);
        return daap;
    }

    /**
     * ShiroFilter<br/>
     * 注意这里参数中的 StudentService 和 IScoreDao 只是一个例子，因为我们在这里可以用这样的方式获取到相关访问数据库的对象，
     * 然后读取数据库相关配置，配置到 shiroFilterFactoryBean 的访问规则中。实际项目中，请使用自己的Service来处理业务逻辑。
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {

        ShiroFilterFactoryBean shiroFilterFactoryBean = new MShiroFilterFactoryBean();
        // 必须设置 SecurityManager  
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
        shiroFilterFactoryBean.setLoginUrl("/account/login");
        // 登录成功后要跳转的连接
        shiroFilterFactoryBean.setSuccessUrl("/account/welcome");
        shiroFilterFactoryBean.setUnauthorizedUrl("/account/403");
//        shiroFilterFactoryBean.set
//        loadShiroFilterChain(shiroFilterFactoryBean);
        return shiroFilterFactoryBean;
    }
    
    /**
     * 有它启动不报错
     * @param authRealm
     * @return
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(AuthRealm authRealm) {
        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
        dwsm.setRealm(authRealm);
        /**
[main]
...
credentialsMatcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
# base64 encoding, not hex in this example:
credentialsMatcher.storedCredentialsHexEncoded = false
credentialsMatcher.hashIterations = 1024
# This next property is only needed in Shiro 1.0\.  Remove it in 1.1 and later:
credentialsMatcher.hashSalted = true

...
myRealm = com.company.....
myRealm.credentialsMatcher = $credentialsMatcher
         */
//      <!-- 用户授权/认证信息Cache, 采用EhCache 缓存 --> 
//        dwsm.setCacheManager(getEhCacheManager());
        return dwsm;
    }
	/**
    * ShiroFilterFactoryBean 处理拦截资源文件问题。
    * 注意：单独一个ShiroFilterFactoryBean配置是或报错的
    * 初始化ShiroFilterFactoryBean的时候需要注入：SecurityManager
    */
//   @Bean
//   public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
//	   
//       ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//       // 必须设置 SecurityManager
//       shiroFilterFactoryBean.setSecurityManager(securityManager);
//       // 拦截器.
//       Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
//
//       //filterChainDefinitionMap.put("/page/*", "authc");
//       // 配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了
//       //filterChainDefinitionMap.put("/security/logoff", "logout");
//       shiroFilterFactoryBean.setLoginUrl("/#/login");
//       // 未授权界面;
//       shiroFilterFactoryBean.setUnauthorizedUrl("/403");
//       
//       shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
//       
//       return shiroFilterFactoryBean;
//   }
//
//   @Bean
//   public SecurityManager securityManager() {
//       DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//       AuthRealm myRealm = new AuthRealm();
//       securityManager.setRealm(myRealm);
//       securityManager.setSessionManager(sessionManager());
////       securityManager.setCacheManager(ehCacheManager());
//       return securityManager;
//   }
//
//	@Bean
//	public SessionManager sessionManager() {
//		DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
////       Collection<SessionListener> listeners = new ArrayList<SessionListener>();
////       listeners.add(new MySessionListener());
////       sessionManager.setSessionListeners(listeners);
//		return sessionManager;
//	}
//   
//	@Bean
//	public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
//	    return new LifecycleBeanPostProcessor();
//	}
//	
//	@Bean
//	public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
//	    DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
//	    daap.setProxyTargetClass(true);
//	    return daap;
//	}
//   @Bean
//   public EhCacheManager ehCacheManager() {
//       EhCacheManager ehCacheManager = new EhCacheManager();
//       //ehCacheManager.setCacheManagerConfigFile("classpath:encache.xml");
//       return ehCacheManager;
//   }
}
